<?php
    class db extends base
    {
        public $conn;
        public $query_count  = 0;
        public $sql_log      = array();
        public $query_result = null;

        private static $instance = '';

        private function __construct()
        {
            $this->conn = mysql_connect(config::get()->db->host, config::get()->db->user, config::get()->db->pwd);

            mysql_select_db(config::get()->db->name);

            $charset = config::get()->db->charset;
            $sql = "SET NAMES '{$charset}'";
            mysql_query($sql);

            return $this;
        }

        /**
		 * 单例入口
         */
        public static function getInstance()
        {
            if (self::$instance != '')
            {
                return self::$instance;
            }
            else
            {
                self::$instance = new self();
                return self::$instance;
            }
        }

        /**
         * 每次执行QUERY_CONUT加1，统计SQL执行次数
         * @param string $sql
         */
        public function query($sql)
        {
            $this->query_count++;
            $this->sql_log[] = $sql;

            $this->query_result = mysql_query($sql, $this->conn);

            return $this;
        }

        /**
         * 获取总数
         */
        public function getCount()
        {
            if (!$this->query_result)
            {
                return 0;
            }

            $tmp = mysql_fetch_row($this->query_result);
            $this->query_result = $tmp[0];

            return $this;
        }

        /**
         * 获取一条记录
         */
        public function fetch_one()
        {
            if (!$this->query_result)
            {
                return array();
            }

            $tmp = array();
            $this->query_result = mysql_fetch_assoc($this->query_result);

            return $this;
        }

        /**
         * 获取所有记录
         */
        public function fetch_all()
        {
            if (!$this->query_result)
            {
                return array();
            }

            $tmp = array();

            while($list = mysql_fetch_assoc($this->query_result))
            {
                $tmp[] = $list;
            }
            $this->query_result = $tmp;

            return $this;
        }

        /**
         * 返回最后一次插入的ID
         */
        public function last_insert_id()
        {
            $last_insert_id = mysql_insert_id($this->conn);

            return $last_insert_id;
        }

        /**
         * 创建表，并初始化一个自动增长的ID列。
         * @param $table_name
         */
        public function create_table($table_name)
        {
            $sql = "CREATE TABLE `{$table_name}` (
                `gid` INT UNSIGNED NOT NULL ,
                `key` CHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
                `value` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
                `style` TINYINT UNSIGNED NOT NULL
                ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci ";

            $this->query($sql);

            return $this;
        }

        /**
         * 添加纵表索引，仅适用于后台模块应用，不通用
         * @param $table_name
         */
        public function add_unique($table_name)
        {
            $sql = "ALTER TABLE `{$table_name}` ADD UNIQUE `gid` ( `gid` , `key` )";

            $this->query($sql);

            return $this;
        }
    }